package oa.hleast.xswl.service.blog.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import oa.hleast.xswl.common.enums.SignSourceTypeEnum;
import oa.hleast.xswl.mapper.blog.UmsBlogTableMapper;
import oa.hleast.xswl.pojo.domain.blog.UmsBlogInfoTable;
import oa.hleast.xswl.pojo.domain.blog.UmsBlogTable;
import oa.hleast.xswl.pojo.vo.blog.UmsBlogTableVo;
import oa.hleast.xswl.service.blog.IBlogInfoTableService;
import oa.hleast.xswl.service.blog.IBlogTableService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 302
 * @date 2021/4/8
 */

@Transactional
@Slf4j
@Service
@AllArgsConstructor
public class BlogTableServiceImpl extends ServiceImpl<UmsBlogTableMapper, UmsBlogTable> implements IBlogTableService {

    private final IBlogInfoTableService blogInfoTableService;

    @Override
    public List<UmsBlogTableVo> topTableList(Long userId) {
        LambdaQueryWrapper<UmsBlogTable> queryWrapper = new LambdaQueryWrapper<UmsBlogTable>()
                .eq(UmsBlogTable::getUserId, userId)
                .orderByDesc(UmsBlogTable::getGmtModified)
                .orderByDesc(UmsBlogTable::getGmtCreate);
        Page<UmsBlogTable> cmsAppsIconPage = this.page(new Page<>(1, 10), queryWrapper);

        if (cmsAppsIconPage.getTotal() == 0) {
            return new ArrayList<>();
        }

        return cmsAppsIconPage.getRecords().stream().map(this::conversionToVo).collect(Collectors.toList());
    }

    @Override
    public List<UmsBlogTableVo> findTableNameByBlogId(Long id) {
        List<UmsBlogInfoTable> blogInfoTableList = blogInfoTableService.list(new LambdaQueryWrapper<UmsBlogInfoTable>().eq(UmsBlogInfoTable::getBlogInfoId, id));
        return blogInfoTableList.stream().map(
                pojo -> {
                    UmsBlogTable table = this.getById(pojo.getTableId());
                    if (ObjectUtils.isNotEmpty(table)) {
                        return conversionToVo(table);
                    }
                    return null;
                }
        ).collect(Collectors.toList());
    }

    @Override
    public UmsBlogTable getClockInTable(SignSourceTypeEnum anEnum) {
        System.out.println(anEnum);
        switch (anEnum) {
            case FLOWER:
                return getFlowerClockInTable();
            case LANDSCAPE:
                UmsBlogTable landscapeClockInTable = getLandscapeClockInTable();
                log.info("landscapeClockInTable = {}", landscapeClockInTable);
                return landscapeClockInTable;
            default:
                log.info("没有相应的打卡来源类型，anEnum = {}", anEnum);
                return null;
        }
    }

    private UmsBlogTable getLandscapeClockInTable() {
        return this.getById(1L);
    }

    private UmsBlogTable getFlowerClockInTable() {
        return this.getById(2L);
    }

    private UmsBlogTableVo conversionToVo(UmsBlogTable umsBlogTable) {
        UmsBlogTableVo umsBlogTableVo = new UmsBlogTableVo();
        BeanUtil.copyProperties(umsBlogTable, umsBlogTableVo);
        return umsBlogTableVo;
    }
}
